package cn.iocoder.yudao.module.member.controller.admin.userinformation;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.member.controller.admin.userinformation.vo.UserInformationPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.userinformation.vo.UserInformationRespVO;
import cn.iocoder.yudao.module.member.controller.app.userinformation.vo.AppUserInformationPageReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.dataobject.userinformation.UserInformationDO;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import cn.iocoder.yudao.module.member.service.userinformation.UserInformationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 用户身份资料列")
@RestController
@RequestMapping("/member/user-information")
@Validated
public class UserInformationController {

    @Resource
    private UserInformationService userInformationService;
    @Resource
    private MemberUserService userService;

    @GetMapping("/get")
//    @Operation(summary = "获得用户身份资料列")
    @PreAuthorize("@ss.hasPermission('member:user-information:query')")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<UserInformationRespVO> getUserInformation(@RequestParam("id") Long id) {
        UserInformationDO userInformation = userInformationService.getUserInformation(id);
        if (ObjectUtil.isNotNull(userInformation)){
            MemberUserDO user = userService.getUser(userInformation.getUserId());
            if(ObjectUtil.isNotNull(user)){
                userInformation.setEnterprise(user.getName());
            }
        }
        return success(BeanUtils.toBean(userInformation, UserInformationRespVO.class));
    }

    @GetMapping("/page")
//    @Operation(summary = "获得用户身份资料列分页")
    @PreAuthorize("@ss.hasPermission('member:user-information:query')")
    public CommonResult<PageResult<UserInformationRespVO>> getUserInformationPage(@Valid UserInformationPageReqVO pageReqVO) {
//        pageReqVO.setUserId(getLoginUserId());
        PageResult<UserInformationDO> pageResult = userInformationService.getUserInformationPage(BeanUtils.toBean(pageReqVO, AppUserInformationPageReqVO.class));
        pageResult.getList().forEach(e->{
            if (ObjectUtil.isNotNull(e)){
                MemberUserDO user = userService.getUser(e.getUserId());
                if(ObjectUtil.isNotNull(user)){
                    e.setEnterprise(user.getName());
                }
            }
        });
        return success(BeanUtils.toBean(pageResult, UserInformationRespVO.class));
    }

    @GetMapping("/export-excel")
//    @Operation(summary = "导出用户身份资料列 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-information:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserInformationExcel(@Valid UserInformationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserInformationDO> list = userInformationService.getUserInformationPage(BeanUtils.toBean(pageReqVO, AppUserInformationPageReqVO.class)).getList();
        list.forEach(e->{
            if (ObjectUtil.isNotNull(e)){
                MemberUserDO user = userService.getUser(e.getUserId());
                if(ObjectUtil.isNotNull(user)){
                    e.setEnterprise(user.getName());
                }
            }
        });
        // 导出 Excel
        ExcelUtils.write(response, "用户身份资料列.xls", "数据", UserInformationRespVO.class,
                        BeanUtils.toBean(list, UserInformationRespVO.class));
    }

}